<template>
    <view>
        <uni-segmented-control :current="menuIndex" :values="menuItems" style-type="text" active-color="#007aff" @clickItem="onClickItem" />

        <uni-list v-if="menuIndex === 0">
            <uni-list-item v-if="storeQuantity.length > 0" v-for="(item, index) in storeQuantity" :key="index">
                <template v-slot:header>
                    <uni-tag :text="index + 1" :type="getNo(index)" size="normal" />
                </template>
                <template v-slot:body>
                    <view style="flex: 1; margin-left: 10px">{{ item?.store?.osName }}</view>
                </template>
                <template v-slot:footer>
                    <view>{{ numberFormat(item.osOrqu, 0) }}</view>
                </template>
            </uni-list-item>
            <u-empty v-if="storeQuantity.length === 0" text="暂无数据"> </u-empty>
        </uni-list>

        <uni-list v-if="menuIndex === 1">
            <uni-list-item v-if="storeAmount.length > 0" v-for="(item, index) in storeAmount" :key="index">
                <template v-slot:header>
                    <uni-tag :text="index + 1" :type="getNo(index)" size="normal" />
                </template>
                <template v-slot:body>
                    <view style="flex: 1; margin-left: 10px">{{ item?.store?.osName }}</view>
                </template>
                <template v-slot:footer>
                    <view>¥{{ numberFormat(item.osOrmo, 2) }}</view>
                </template>
            </uni-list-item>
            <u-empty v-if="storeAmount.length === 0" text="暂无数据"> </u-empty>
        </uni-list>
    </view>
</template>

<script>
import { getStoreRanks } from "../../utils/api";
import { mapGetters } from "vuex";
import { getNo } from "../../utils/helper.js";
export default {
    data() {
        return {
            menuItems: ["门店订单数", "门店订单金额"],
            menuIndex: 0,
            storeQuantity: [],
            storeAmount: [],
        };
    },
    onLoad() {
        this.loadStoreRank({ gpUuid: this.activity.gpUuid, orderBy: "OS_ORQU_DESC" });
    },
    computed: {
        ...mapGetters({
            activity: "activityInfo/activity",
        }),
    },
    methods: {
        onClickItem(e) {
            if (this.menuIndex !== e.currentIndex) {
                this.menuIndex = e.currentIndex;
                if (e.currentIndex == 0) {
                    this.loadStoreRank({ gpUuid: this.activity?.gpUuid, orderBy: "OS_ORQU_DESC" });
                }
                if (e.currentIndex == 1) {
                    this.loadStoreRank({ gpUuid: this.activity?.gpUuid, orderBy: "OS_ORMO_DESC" });
                }
            }
        },
        async loadStoreRank({ gpUuid, orderBy }) {
            let res = await getStoreRanks({ gpUuid, orderBy });
            if (orderBy === "OS_ORQU_DESC") this.storeQuantity = res?.ranks?.filter((item) => item.osOrqu > 0) || [];
            if (orderBy === "OS_ORMO_DESC") this.storeAmount = res?.ranks?.filter((item) => item.osOrmo > 0) || [];
        },
    },
};
</script>

<style></style>
